<?php 
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Model\Article;
use App\Model\Nav;
use Session;

class EditController extends Controller
{
	public function __construct()
    	{
    		$this->middleware('admincheck');
    		$this->middleware('loadnav');
	}

	public function Index()
	{
		return view('admin.edit');
	}

	public function store(Request $request)
	{
		$this->validate($request, [
		 	'title' => 'required',
		 	'content' => 'required',
		]);
		$data['body'] = $request->input('content');
		$data['title'] = $request->input('title');
		$data['click_num'] = 0;
		$Article = Article::Add_Article($data);
		return redirect('/W/'.$Article->id);
	}

	public function update($id, Request $request)
	{
		$this->validate($request, [
		 	'name' => 'required',
		 	'href' => 'url',
		]);
		$data['name'] = $request->input('name');
		$data['href'] = $request->input('href');
		$data['sort'] = $request->input('sort');
		if ($id == '0') {
			Nav::Add($data);
		} else {
			Nav::Update_Nav($id, $data);
		}
		return redirect('/Edit');
	}

	public function destroy($id)
	{
		return response()->json(['status' => Nav::Destroy_Nav($id)]);
	}
}